/*LICENSE_BEGIN
 * ******************************************************************
 * Copyright 2006 by the Dainamite Robocup-Team at the
 * DAI-Labor, TU-Berlin (www.dai-labor.de)
 * 
 * Members of Dainamite are:
 * Holger Endert
 * Robert Wetzker
 * Thomas Karbe
 * Axel Heßler
 * Philippe Bütter
 * Felix Brossmann
 * 
 * Licensed under the GNU GPL (General Public License) Free Software 
 * License, Version 2. You may use this file only in compliance with 
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.fsf.org/licensing/licenses/gpl.txt
 * ******************************************************************
LICENSE_END*/
package robocup.component.tactics;

import robocup.component.ControllerNumbers.PLAY_MODE;
import robocup.component.actionfactories.AttentionToActionFactory;
import robocup.component.actionfactories.TurnNeckActionFactory;
import robocup.component.situations.DribbleSituation;

/**
 * The state representing the players dribble intention. This state is nearly
 * always triggered when the player has the ball under control. The decision
 * wether to dribble, pass, score etc. is mostly based on these states' 
 * assessments.
 */
public class DribbleState extends AbstractState {

	/**
	 * State which causes the Player to Dribble
	 * 
	 * @param parent -
	 *            the StateEvaluation (parent) object
	 */
	public DribbleState(StateEvaluation parent) {

		super(parent);
		this.state = STATES.DRIBBLE;
	}

	/**
	 * Precondition is set up by player has ball, defined in AbstractState.
	 */
	@Override
	public boolean preCondition() {

		if (!checkPlayMode()) {
			return false;
		}

		//used only for penalty
		if (getWorld().getNumber() == 10
				&& PLAY_MODE.META_PENALTY_OWN
						.contains(getWorld().getPlayMode())) {
			if (getWorld().canKick()) {
				return true;
			} else {
				return false;
			}
		}

    //used only for penalty
		if (PLAY_MODE.META_PENALTY_OWN.contains(getWorld().getPlayMode())) {
			return false;
		}

		boolean b = (checkPlayMode() 
        && checkMyBall() 
        && this.getProphet().getScoreBenefit() < 0.80);

		return b;
	}

	/**
	 * Dummy. All done in the {@link DribbleSituation}
	 */
	@Override
	protected double successProbability() {
		return 1.0;
	}

	/**
	 * This method calcultes the estimated benefit when dribbling. The estimate
	 * depends on the distance to the other goal, nearer is better.
   * 
   * @see DribbleSituation
	 */
	@Override
	protected double successBenefit() {
		return this.getProphet().getDribbleBenefit();
	}

  /**
   * @see DribbleSituation
   */
	@Override
	public void calculateMainAction() {
		this.subStateString = empty;
		mainAction = this.getProphet().getBestDribbleAction();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see robocup.component.tactics.AbstractState#setSubStateString()
	 */
	@Override
	public void setSubStateString() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see robocup.component.tactics.AbstractState#calculateTurnNeckAction()
	 */
	@Override
	public void calculateTurnNeckAction() {

    //if dribbling near the opponents goal the goalie may be the opponent to 
    //watch
		if (this.getProphet().shouldWatchGoalie()) {
			turnNeckAction = TurnNeckActionFactory.getAction(getWorld(),
					STATES.DRIBBLE, mainAction, viewModeAction);
		} else {
			super.calculateTurnNeckAction();
		}
	}


}
